package mvc.elicoide;

import java.awt.event.MouseEvent;

import org.geom.Maths;
import org.geom.Point2D;
import org.geom.curve.Curve;
import org.geom.curve.helix.Elicoide;

import mvc.controller.MouseController;
import mvc.model.ModelCurve;
import mvc.view.ViewCurve;


public class ControllerElicoide2D extends MouseController{
		
	
	private float p = 0;

	private boolean updateModel;

	//---------------------------------------------------------------------- contructor
	
	public ControllerElicoide2D(ModelCurve model, ViewCurve view, String param) {
		super(model, view, param);
	}
	
	//---------------------------------------------------------------------- notify model

	protected void notifyElicoideR(float t) {
		((IModelElicoide)model).setR(t);
	}
	
	protected void notifyElicoideK(float t) {
		((IModelElicoide)model).setK(t);
	}

	//---------------------------------------------------------------------- interface

	@Override
	public void mousePressed(MouseEvent e) {
		super.mousePressed(e);
		
		updateModel = (param!="" && nearCenter(mouseDown)) ? true : false;
		mouseDragged(e);

		model.notifyAllViews();
	}
	
	@Override
	public void mouseDragged(MouseEvent e) {
		super.mouseDragged(e);
		p = Maths.mapProportion(deltay, 0, view.getHeight(), 0, 100);// / 50;
		
		if(updateModel) {
			if (param==Elicoide.R) notifyElicoideR(p);
			if (param==Elicoide.K) notifyElicoideK(p);
		} else {
			view.setTranslate(deltax, deltay);
			view.repaint();
		}
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		model.setCurrentPoint(Maths.mapProportion(e.getY(), 
									0, view.getHeight(), 
									0, Curve.length*model.getArc()));
		//System.out.println("ControllerElicoide2D > mouseMoved > dudee");
		view.repaint();
	}
	
	//---------------------------------------------------------------------- privates
	
	private boolean nearCenter(Point2D p) {
		Point2D p2 = view.getTranslate();
		return  Point2D.dist(p.x, p.y, p2.x, p2.y)<50;
	}	
		
}